<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Piano Sound Simulation</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        input[type="number"] {
            padding: 10px;
            width: 350px;
            font-size: 16px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            margin: 10px;
        }
        #output {
            margin-top: 20px;
            white-space: pre-wrap;
        }
        #popup {
            position: fixed;
            top: 50%;
            left: 50%;
			font-size: 156px;
            transform: translate(-50%, -50%);
            background-color: rgba(255, 255, 255, 0.9);
            border: 1px solid #ccc;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: none;
            z-index: 1000;
        }
    </style>
</head>
<body>
    <h1>音阶练习</h1>
    <input type="number" id="inputN" placeholder="输入每轮的音阶数">
    <button onclick="startSimulation()">开始</button>
    <button onclick="stopSimulation()">暂停</button>
    <div id="output"></div>
    <div id="popup"><span id="popupText"></span></div>

    <script>
        const voices = {
            1: "Do",
            2: "Re",
            3: "Mi",
            4: "Fa",
            5: "So",
            6: "La",
            7: "Si"
        };

        let intervalId;
        let n;
        let currentIteration = 0;

        function startSimulation() {
            n = parseInt(document.getElementById('inputN').value);
            if (isNaN(n) || n <= 0) {
                alert("请输入一个有效的正整数");
                return;
            }
            currentIteration = 0;
            simulateNextStep();
        }

        function stopSimulation() {
            clearInterval(intervalId);
            location.reload(); // Refresh the page when stopping the simulation
        }

        function simulateNextStep() {
            if (currentIteration >= n) {
                showRandomNumbersAndNotes(n);
                currentIteration = 0;
            } else {
                showRandomNumberAndNote();
            }
        }

        function showRandomNumberAndNote() {
            const randomNumber = Math.floor(Math.random() * 7) + 1;
            showPopup(randomNumber);
            setTimeout(() => {
                showPopup(voices[randomNumber]);
            }, 3000);
            currentIteration++;
            setTimeout(simulateNextStep, 6000); // Total time for one iteration is 6 seconds
        }

        function showRandomNumbersAndNotes(count) {
            const randomNumbers = [];
            for (let i = 0; i < count; i++) {
                randomNumbers.push(Math.floor(Math.random() * 7) + 1);
            }
            showPopup(randomNumbers.join(" "));
            setTimeout(() => {
                showPopup(randomNumbers.map(num => voices[num]).join(" "));
            }, count * 1000);
            setTimeout(simulateNextStep, (count + 5) * 1000); // Total time for this step is (N+5) seconds
        }

        function showPopup(text) {
            const popup = document.getElementById('popup');
            const popupText = document.getElementById('popupText');
            popupText.textContent = text;
            popup.style.display = 'block';
            setTimeout(() => {
                popup.style.display = 'none';
            }, 2000);
        }
    </script>
</body>
</html>
